package com.neusoft.carsale.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.carsale.bean.Result;
import com.neusoft.carsale.bean.User;
import com.neusoft.carsale.service.UserService;

/**
 * 
 * @author Administrator
 *
 */
@Controller
public class LoginController {
	Logger logger = Logger.getLogger(LoginController.class);
	@Autowired
	private UserService userService;

	@RequestMapping("login")
	@ResponseBody
	public Result login(User user, String validate, HttpSession session) {
		Result result = new Result(Boolean.FALSE, "登录失败");
		String rand = (String) session.getAttribute("rand");
		if (rand != null && rand.equalsIgnoreCase(validate)) {
			User loginUser = userService.login(user);
			if(loginUser!=null) {
				logger.info("登录成功"+loginUser);
				session.setAttribute("loginUser", loginUser);
				result=new Result(Boolean.TRUE,"登录成功");
			}
		}else {
			result=new Result(Boolean.FALSE,"验证码错误");
		}
		return result;

	}
	@RequestMapping("logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/login.jsp";
	}
	
	

}
